﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;

using DNA;
using TJADRICD.CollaborativeDesignPlugin.Form;
using TJADRICD.CollaborativeDesignPlugin.Function;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.PlottingServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace TJADRICD.CollaborativeDesignPlugin
{
    class CommandDebug
    {
        //Check data and config info
        [CommandMethod("Test01")]
        public static void Test01()
        {
            Tools.WriteMessageWithReturn("Data.ProgramData:" + Data.ProgramData);
            Tools.WriteMessageWithReturn("Data.DefaultWebConfigFile:" + Data.DefaultWebConfigFile);
            Tools.WriteMessageWithReturn("Data.DefaultHandwritingPath:" + Data.DefaultHandwritingPath);
            {
                Dictionary<String, String>.Enumerator enumerator = Data.PaperInfo.GetEnumerator();
                do
                {
                    KeyValuePair<String, String> pair = enumerator.Current;
                    Tools.WriteMessageWithReturn("Data.PaperInfo.Name:" + pair.Key + " Data.PaperInfo.Paper:" + pair.Value);
                } while (enumerator.MoveNext());

            }
            {
                Dictionary<String, String>.Enumerator enumerator = Data.MachineInfo.GetEnumerator();
                do
                {
                    KeyValuePair<String, String> pair = enumerator.Current;
                    Tools.WriteMessageWithReturn("Data.MachineInfo.Name:" + pair.Key + " Data.MachineInfo.Machine:" + pair.Value);
                } while (enumerator.MoveNext());

            }
            Tools.WriteMessageWithReturn("Config.LocalConfigFile:" + Config.LocalConfigFile);
            Tools.WriteMessageWithReturn("Config.WebConfigFile:" + Config.WebConfigFile);
            Tools.WriteMessageWithReturn("Config.HandwritingPath:" + Config.HandwritingPath);
            Tools.WriteMessageWithReturn("Config.FramePath:" + Config.FramePath);
            Tools.WriteMessageWithReturn("Config.ProjectInfoPath:" + Config.ProjectInfoPath);
            foreach (String temp in Config.ProjectPath)
            {
                Tools.WriteMessageWithReturn("Config.ProjectPath:" + temp);
            }
            Tools.WriteMessageWithReturn("Config.StdFrameInfoFile:" + Config.StdFrameInfoFile);
        }

        [CommandMethod("Test02")]
        public static void Test02()
        {
            PromptStringOptions string_option = new PromptStringOptions("输入字符串");
            PromptResult result = Tools.Editor.GetString(string_option);
            String text = Function.Function_Hash.GetNameMD5(result.StringResult);
            Tools.WriteMessageWithReturn(text);            
        }

        [CommandMethod("Test03")]
        public static void Test03()
        {
            if(File.Exists(@".\TJAD_MD5.dll"))
            {
                Tools.WriteMessageWithReturn("Exists");
            }
            else
            {
                Tools.WriteMessageWithReturn("Not Exists");
                DirectoryInfo dir_info = new DirectoryInfo(@".\");
                Tools.WriteMessageWithReturn(dir_info.FullName);
            }            
            
        }

        [CommandMethod("Test04")]
        public static void Test04()
        {
            Function.Function_Registry.GetOptions();
        }

        [CommandMethod("Test05")]
        public static void Test05()
        {
            Dictionary<String, String> list = Function_Registry.GetOptions();
            String PrinterConfigDir = list["PrinterConfigDir"];
            String PrinterDescDir = list["PrinterDescDir"];
            String PrinterStyleSheetDir = list["PrinterStyleSheetDir"];

            Boolean flag_copy = true;

            if (flag_copy)
            {
                FileInfo ApplicationPath = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
                DirectoryInfo Plotters = new DirectoryInfo(ApplicationPath.Directory.FullName + @"\" + "Plotters");

                Dictionary<String, String>.Enumerator enumerator = Data.MachineInfo.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    String machine = enumerator.Current.Value;
                    MD5 md5Hasher = MD5.Create();
                    FileInfo src_pc3_file = new FileInfo(Plotters.FullName + @"\" + machine);
                    FileInfo src_pmp_file = new FileInfo(Plotters.FullName + @"\" + machine.Replace(".pc3", ".pmp"));
                    FileInfo dst_pc3_file = new FileInfo(PrinterConfigDir + @"\" + src_pc3_file.Name);
                    FileInfo dst_pmp_file = new FileInfo(PrinterDescDir + @"\" + src_pmp_file.Name);

                    if (!dst_pc3_file.Exists)
                    {
                        src_pc3_file.CopyTo(dst_pc3_file.FullName, true);
                    }
                    byte[] data1 = md5Hasher.ComputeHash(src_pc3_file.OpenRead());
                    byte[] data2 = md5Hasher.ComputeHash(dst_pc3_file.OpenRead());
                    StringComparer c = StringComparer.OrdinalIgnoreCase;
                    if (c.Compare(System.Text.Encoding.Default.GetString(data1), System.Text.Encoding.Default.GetString(data2)) == 0)
                    {
                        MessageBox.Show("Same pc3 Files"+"\n"+src_pc3_file.FullName + "\n" + dst_pc3_file.FullName);
                    }
                    else
                    {
                        MessageBox.Show("Different pc3 Files" + "\n" + src_pc3_file.FullName + "\n" + dst_pc3_file.FullName);
                    }


                    if (!dst_pmp_file.Exists)
                    {
                        src_pmp_file.CopyTo(dst_pmp_file.FullName, true);
                    }
                    data1 = md5Hasher.ComputeHash(src_pmp_file.OpenRead());
                    data2 = md5Hasher.ComputeHash(dst_pmp_file.OpenRead());
                    if (c.Compare(System.Text.Encoding.Default.GetString(data1), System.Text.Encoding.Default.GetString(data2)) == 0)
                    {
                        MessageBox.Show("Same pmp Files" + "\n" + src_pmp_file.FullName + "\n" + dst_pmp_file.FullName);
                    }
                    else
                    {
                        MessageBox.Show("Different pmp Files" + "\n" + src_pmp_file.FullName + "\n" + dst_pmp_file.FullName);
                    }
                }
            }

        }

        [CommandMethod("TJADRICDCOMMAND")]
        public static void TJADRICDCOMMAND()
        {
            Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;

            Boolean flag_exit = false;            
            while(!flag_exit)
            {
                PromptResult result = editor.GetString("TJADRICD:");
                if (table.ContainsKey(result.StringResult))
                {
                    table[result.StringResult].run();
                }
                else
                {
                    TJLog(@"can't find this function");
                }
            }           
        }

        public static void TJLog(String message)
        {
            Tools.WriteMessageWithReturn(message);
        }

        public static Dictionary<String, TJCommandPrompt> table = new Dictionary<String, TJCommandPrompt>();

        public static void Initial()
        {
            table.Add("font", new TJFont());
        }

        public abstract class TJCommandPrompt
        {
            public abstract void run();
        }

        public sealed class TJFont : TJCommandPrompt
        {
            public override void run()
            {
                Database db = Application.DocumentManager.MdiActiveDocument.Database;
                Transaction tr = Tools.Document.TransactionManager.StartTransaction();
                TextStyleTable tst = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                foreach (ObjectId id in tst)
                {
                    TextStyleTableRecord tstr = tr.GetObject(id, OpenMode.ForRead) as TextStyleTableRecord;
                    TJLog(tstr.BigFontFileName);
                }
            }
        }
    }
}
